<?php

class Emms_WrittenOffLoan
{
    private $_d;    
    
    public function __construct() 
    {
        $id = 0;
        $args = func_get_args(); 
        if(isset($args[0])&&is_numeric($args[0])) {
            $id = $args[0];
        } elseif(isset($args[0])&&is_array($args[0])) {
            if(isset($args[0]['WriteOffPaymentId'])) {
                $lwops = new LoanWriteOffPayments();
                $id = $lwops->getRecord($args[0]['WriteOffPaymentId'])->d->LoanId;
            } elseif (isset($args[0]['WriteOffChargeId'])) {
                $lwocs = new LoanWriteOffCharges();
                $id = $lwocs->getRecord($args[0]['WriteOffChargeId'])->d->LoanId;
            }
        } else {
            throw new Exception($this->translate("Invalid loan code or parameters")); 
        }
        $loans = new LoanWriteOffs();
        try {
            $loan = $loans->getRecord($id);
        } catch (Exception $e) {
            throw new Exception($e->getMessage()); 
        }
        $this->_d = (object) $loan->d->toArray();
        $helper = new HTech_View_Helper_ArraySumByIndex();
        
        $this->_d->LegalCharges = $helper->arraySumByIndex($loan->r->charges->toArray(),'Amount');
        $this->_d->LegalPayments = $helper->arraySumByIndex($loan->r->payments->toArray(),'Amount');
        $this->_d->LegalBalance = $this->_d->LegalCharges - $this->_d->LegalPayments;
                
        $this->_d->PrincipalPayments = $helper->arraySumByIndex($loan->r->regularPayments->toArray(),'Principal');
        $this->_d->LoanInsurancePayments = $helper->arraySumByIndex($loan->r->regularPayments->toArray(),'LoanInsurance');
        $this->_d->HealthInsurancePayments = $helper->arraySumByIndex($loan->r->regularPayments->toArray(),'HealthInsurance');
        $this->_d->AdministrativeFeePayments = $helper->arraySumByIndex($loan->r->regularPayments->toArray(),'AdministrativeFee');
        $this->_d->InterestPayments = $helper->arraySumByIndex($loan->r->regularPayments->toArray(),'Interest');
        $this->_d->PenaltiesPayments = $helper->arraySumByIndex($loan->r->regularPayments->toArray(),'Penalties');
        $this->_d->RegularPayments = $helper->arraySumByIndex($loan->r->regularPayments->toArray(),'Pmt');
        
        $this->_d->InterestCharges = $this->_d->InterestPayments + $this->_d->Interest;
        $this->_d->PenaltiesCharges = $this->_d->PenaltiesPayments + $this->_d->Penalties;
        
        $this->_d->RegularCharges = $this->_d->PrincipalCharges + $this->_d->LoanInsuranceCharges +
                $this->_d->HealthInsuranceCharges + $this->_d->AdministrativeFeeCharges + 
                $this->_d->InterestPayments + $this->_d->Interest +
                $this->_d->PenaltiesPayments + $this->_d->Penalties;
        
        $this->_d->TotalBalance = $this->_d->PrincipalBalance +
            $this->_d->LoanInsuranceBalance +
            $this->_d->HealthInsuranceBalance +
            $this->_d->AdministrativeFeeBalance +
            $this->_d->InterestBalance +
            $this->_d->PenaltiesBalance +
            $this->_d->LegalBalance ;
    }
    
    public function get($prp)
    {
        return isset($this->_d->$prp) ?
            $this->_d->$prp : null;
    }
    
    public function checkStatus()
    {
        if($this->get('Status') == 'Written-Off') {
            if($this->get('TotalBalance') <= 0) {
                Zend_Registry::get('db')->query('update loans set Status = ? where LoanId = ?', 
                    array('Recovered', $this->get('LoanId')));
                Zend_Registry::get('db')->query('insert into loan_status_changes
                    (LoanId,PreviousStatus,Status,Date,DateTime,UserId) values (?,?,?,?,?,?)', 
                    array(
                        $this->get('LoanId'),
                        'Written-Off',
                        'Recovered',
                        date('Y-m-d'),
                        date('Y-m-d H:i:s'),
                        Zend_Auth::getInstance()->getIdentity()->UserId));
                /*
                $lsc = new LoanStatusChanges();
                $lscRow = $lsc->createRow();
                $lscRow->LoanId = $this->get('LoanId');
                $lscRow->PreviousStatus = 'Written-Off';
                $lscRow->Status = 'Recovered';
                $lscRow->Date = date('Y-m-d');
                $lscRow->DateTime = date('Y-m-d H:i:s');
                $lscRow->UserId = Zend_Auth::getInstance()->getIdentity()->UserId;
                $lscRow->save();
                * 
                */
            }
        }
    }
    
}
    
